<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/11/3
 * Time: 18:00
 */
namespace app\index\controller;


use app\common\model\ImageModel;
use app\common\model\OpinionModel;

use app\common\model\ReplyModel;
use app\index\model\UserModel;
use app\index\validate\Opinion;
use mg_api\Mg_api;

class Feedback extends Common
{

    public function feedback_type()
    {
        $type = input('param.type');
        //项目id
        $project_id = mb_convert_encoding(input('param.project_id'),"utf-8", "gbk");

        if ($type == 1) {
            //项目反馈
            $data = feedback_type_1();
        } else if ($type == 2) {
            //反馈
            $data = feedback_type_2();
        }

        $this->assign('project_id', $project_id);

        $this->assign('data', $data);
        //反馈类型
        $this->assign('type', $type);
        return view('feedback_2');
    }

    public function from($title_id, $type, $project_id)
    {
		$project_id = mb_convert_encoding($project_id,"utf-8", "gbk");
        $user =  UserModel::find($_SESSION['user_id']);
        $tel = explode('/', $user['user_tel']);

        if ($type == 1) {
            //项目反馈
            $data = feedback_type_1();
            $project_id = $project_id;

        } else if ($type == 2) {
            //反馈
            $data = feedback_type_2();
            $project_id = $project_id;
        }
        $user = UserModel::find($_SESSION['user_id']);
        $this->assign('user', $user);
        $this->assign('project_id', $project_id);
        $title = $data[$title_id];
        $this->assign('title_id', $title_id);
        $this->assign('title', $title);
        $this->assign('type', $type);
        $this->assign('tel',$tel);
        return view('from');
    }

    public function create()
    {
        if (request()->isPost()) {
            $files = request()->file('file');
            if ($files) {
                $re = $this->img_up($files);
                $src = implode(',', $re);
            } else {
              $src = "";
            }

            $validate = new Opinion();
            $result = $validate->check($_POST);
            if ($result) {
                $re = OpinionModel::create([
                    'type' => $_POST['type'],
                    'title' => $_POST['title'],
                    'content' => $_POST['content'],
                    'tel' => $_POST['tel'],
                    'user_id' => $_SESSION['user_id'],
                    'created_at' => time(),
                    'project_id' => $_POST['project_id'],
                    'src' => $src
                ]);

                if ($re) {
                    $this->success("提交成功", 'index/index/index');
                } else {
                    $this->error("提交失败");
                }
            } else {
                $this->error($validate->getError());
            }
        } else {
            $this->error('非法数据');
        }
    }

    //图片上传
    public function img_up($files)
    {
        if ($files) {
            $srcs = [];
            foreach ($files as $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'feedback');
                if ($info) {
                    $src = "/uploads/feedback/" . $info->getSaveName();
                    // 成功上传后 获取上传信息
                    //存入图片表
                   $re = ImageModel::create([
                        'src'=>$src,
                        'created_at'=>time()
                    ]);
                  $srcs[] = $re->id;

                } else {
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
            return $srcs;
        }

    }
    //历史消息
    public function historical_reply()
    {
        //普通反馈
        $re = OpinionModel::where('type', '2')
            ->where('user_id',$_SESSION['user_id'])
            ->select();
        $title_name = feedback_type_2();
        $reply = [];
        foreach ($re as $k=>$v) {
            //判断是否有回复消息
            $count = ReplyModel::where('opinion_id',$v->id)->count();
            if($count){
                $v->title = $title_name[$v->title];
                $reply[] = $v;
            }
        }
        $this->assign('reply', $reply);
        return view('historical_reply');
    }

    public function reply_details($id){
        $opinion = OpinionModel::get($id);
        $title_name = feedback_type_2();
        $opinion->title = $title_name[$opinion->title];

        //获取回复信息
        $reply = ReplyModel::where('opinion_id',$id)->find();
        //如果此详细是未查看的就改变状态
        if($reply['is_look'] == '0'){
            $up_reply = ReplyModel::get($reply->id);
            $up_reply->is_look = '1';
            $up_reply->save();
        }
        $this->assign('opinion',$opinion);
        $this->assign('reply',$reply);
        return view('reply_details');


    }

}